<template>
  <div class="mod-config">
    <div v-if="bookinfos">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.bookName" placeholder="专题标题" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="dataForm.author" placeholder="专栏作者" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="search()">查询</el-button>
          <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        style="width: 100%;"
      >
        <el-table-column prop="id" header-align="center" align="center" label="ID"></el-table-column>
        <el-table-column prop="bookName" header-align="center" align="center" label="专栏标题"></el-table-column>
        <el-table-column prop="author" header-align="center" align="center" label="专栏作者"></el-table-column>
        <el-table-column prop="speaker" header-align="center" align="center" label="作者简介"></el-table-column>
        <el-table-column header-align="center" align="center" label="封面图">
          <template slot-scope="scope">
            <img :src="scope.row.coverUrl" style="width:50px">
          </template>
        </el-table-column>
        <el-table-column prop="briefIntroduction" header-align="center" align="center" label="推荐文案"></el-table-column>
        <el-table-column prop="gmtCreated" header-align="center" align="center" label="创建时间"></el-table-column>
        <el-table-column prop="gmtUpdate" header-align="center" align="center" label="更新时间"></el-table-column>
        <el-table-column prop="bookVisit" header-align="center" align="center" label="播放量"></el-table-column>
        <el-table-column prop="gmtCreated" header-align="center" align="center" label="状态">
          <template slot-scope="scope">
            <span>{{scope.row.isAlive==0?'下架':''}}</span>
            <span>{{scope.row.isAlive==1?'上架':''}}</span>
            <span>{{scope.row.isAlive==2?'定时上架':''}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="isRecommend" header-align="center" align="center" label="开启首页推荐">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isRecommend"
              @change="(value)=> {return switchmethod(value, scope.row.id, scope.row.recommendId)}"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="seebookChapter(scope.row)">查看</el-button>
            <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row)">修改</el-button>
            <el-button type="text" size="small" @click="jiaHandle(scope.row.id, scope.row.bookName, scope.row.isAlive, scope.row.publishTime)">上/下架</el-button>
            <el-button type="text" size="small" @click="deltetHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      ></el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
      <!-- 查看 -->
      <book-capter v-if="captersee" ref="captersees" @refreshDataList="getDataList"></book-capter>

      <el-dialog
        title="上/下架"
        :close-on-click-modal="false"
        :visible.sync="visible"
      >
        <el-form
          :model="dataForm1"
          ref="dataForm1"
          @keyup.enter.native="dataFormSubmit()"
          label-width="110px"
        >
          <el-form-item label="上/下架" prop='isAlive'>
            <el-radio v-model="dataForm1.isAlive" :label="1">立即上架</el-radio>
            <el-radio v-model="dataForm1.isAlive" :label="2">定时上架</el-radio>
            <el-date-picker
              popper-class = 'dingTime'
              v-if="dataForm1.isAlive === 2"
              v-model="dataForm1.publishTime"
              type="datetime"
              placeholder="选择推送时间"
              style="margin-left: 20px;"
              @change="changeTime"
              value-format="yyyy-MM-dd HH:mm:ss"
            >
            </el-date-picker>
            <el-radio style="display: block;margin-left: 0px;margin-top: 5px;" v-model="dataForm1.isAlive" :label="0">下架</el-radio>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        <el-button @click="visible = false">取消</el-button>

      </span>
      </el-dialog>

    </div>
  </div>
</template>

<script>
  import AddOrUpdate from "./add";
  import BookCapter from "./detail";

  export default {
    data() {
      return {
        dataForm: {
          key: "",
          id: ""
        },
        dataForm1: {
          isAlive: '',
          publishTime: ''
        },
        visible: false,
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        captersee: false,
        bookinfos: true
      };
    },
    components: {
      AddOrUpdate,
      BookCapter
    },
    // activated () {
    //   this.init();

    // },
    props: ["id", "hasVideo"],
    watch: {
      bookId() {
        return this.id;
      },
      hasVideo() {
        return this.hasVideo;
      }
    },
    mounted() {
      this.getDataList();
    },
    methods: {
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl("/course/bookinfo/famousColumn/list"),
          method: "get",
          params: this.$http.adornParams({
            page: this.pageIndex,
            limit: this.pageSize,
            bookName: this.dataForm.bookName,
            author: this.dataForm.author
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list;
            this.totalPage = data.page.totalCount;
            data.page.list.map((item, index)=> {
              this.dataList[index].isRecommend = item.isRecommend === 1
            })
          } else {
            this.dataList = [];
            this.totalPage = 0;
          }
          this.dataListLoading = false
        });
      },
      search() {
        this.pageIndex = 1
        this.getDataList()
      },
      dataFormSubmit() {
        this.$http({
          url: this.$http.adornUrl(
            `/course/bookinfo/famousColumn/upOrDown`
          ),
          method: "post",
          data: this.$http.adornData(this.dataForm1)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.getDataList()
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      },
      deltetHandle(id) {
        this.$confirm(
          `确定删除该专栏?`,
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
        ).then(() => {
          this.$http({
            url: this.$http.adornUrl("/course/bookinfo/famousColumn/delete"),
            method: "post",
            data: this.$http.adornData({id})
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        });
      },
      changeTime () {
        if (new Date(this.dataForm1.pushTime).getTime() < new Date().getTime()) {
          this.dataForm1.pushTime = ''
          this.$message.error('所选时间要大于当前时间')
        }
      },
      switchmethod(val, id, recommendId) {
        this.$http({
          url: this.$http.adornUrl(
            `/course/bookinfo/famousColumn/updateRecommend`
          ),
          method: "post",
          data: this.$http.adornData({bookId: id, isAlive: val?1:0, recommendId: recommendId})
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val;
        this.pageIndex = 1;
        this.getDataList();
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val;
        this.getDataList();
      },
      // 新增 / 修改
      addOrUpdateHandle(obj) {
        this.addOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(obj);
        });
      },
      jiaHandle(id, bookName, isAlive, publishTime) {
        this.visible = true
        this.dataForm1.bookId = id
        this.dataForm1.bookName = bookName
        this.dataForm1.isAlive = isAlive
        this.dataForm1.publishTime = publishTime
      },
      //查看
      seebookChapter(obj) {
        this.captersee = true;
        this.$nextTick(() => {
          this.$refs.captersees.init(obj);
        });
      },
    }
  };

  // 排序
  function sorting(min) {
    return function (obj1, obj2) {
      var value1 = obj1[min];
      var value2 = obj2[min];
      return value1 - value2;
    };
  }
</script>
